package com.delio.client.produccion.recetas;


import java.util.ArrayList;  
import java.util.List;  
  
import com.delio.client.idioma.DelioConstants;
import com.delio.client.produccion.articulos.Articulos;
import com.delio.client.produccion.detallerecetas.DetalleReceta;
import com.delio.client.produccion.detallerecetas.DetalleRecetas;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.dnd.GridDragSource;  
import com.extjs.gxt.ui.client.dnd.GridDropTarget;  
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;  
import com.extjs.gxt.ui.client.widget.ContentPanel;  
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;  
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;  
import com.extjs.gxt.ui.client.widget.grid.Grid;  
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid; 
import com.google.gwt.core.client.GWT;


public class RecetaGeneralForm extends ContentPanel{
	@SuppressWarnings("unused")
	private DelioConstants x = GWT.create(DelioConstants.class);
	private GridDropTarget target,target2,target3;  
	ListStore<ModelData> store,store2,store3;
	Button btnSeleccionar;
	Grid<ModelData> grid;
	EditorGrid<ModelData> grid2,grid3;
	Button btnSave;//,btnNewRecipe;
	  
	  
   public RecetaGeneralForm(){	
	setHeaderVisible(false);
	BorderLayout layout = new BorderLayout();
	setLayout(layout);   
	Articulos.getStore();
	Recetas.getPaginate();
  	DetalleRecetas.getPaginate();
	//Recetas.getStore();

	ContentPanel cp = new ContentPanel(); 
	cp.setHeaderVisible(true);
	cp.setHeading("Listado Insumos");
	cp.setIconStyle("images/icons/table.png");

  /*  ToolBar toolBar = new ToolBar();  
    toolBar.add(new LabelToolItem(x.cliente()));  
    final SimpleComboBox<String> type = new SimpleComboBox<String>();  
    type.setTriggerAction(TriggerAction.ALL);  
    type.setEditable(false);  
    type.setWidth(100);  
    type.add("Append");  
    type.add("Insert");  
    type.setSimpleValue("Append");  
    type.addListener(Events.Change, new Listener<FieldEvent>() {  
      public void handleEvent(FieldEvent be) {  
        boolean append = type.getSimpleValue().equals("Append");  
        Feedback f = append ? Feedback.APPEND : Feedback.INSERT;  
        target.setFeedback(f);  
        target2.setFeedback(f);  
      }  
    });  
  
    toolBar.add(type);  
    setTopComponent(toolBar);*/
    
    store = new ListStore<ModelData>();
    //store = Recetas.storeCombo;
    store = Articulos.storeCombo;

    
    
    grid = new Grid<ModelData>(store, createColumnModel());
    grid.setBorders(false);  
   // grid.setAutoExpandColumn("artcodigo");  
    grid.setBorders(true);  
////////////////////////////////////////////////////////////////////
    BorderLayoutData west  = new BorderLayoutData(LayoutRegion.WEST,300);
    west.setMargins(new Margins(0,0,0,0));
    west.setCollapsible(true);
    west.setSplit(true);
    west.setCollapsible(true);
    
////////////////////////////////////////////////////////////////////    
    BorderLayoutData center  = new BorderLayoutData(LayoutRegion.CENTER,400);//nada
    center.setMargins(new Margins(0,5,0,0));
    center.setCollapsible(true);
    center.setSplit(true);
    center.setCollapsible(true);
      
    BorderLayoutData east = new BorderLayoutData(LayoutRegion.EAST,400);//700
    east.setMargins(new Margins(0,0,0,0));
    east.setSplit(false);
    east.setCollapsible(false);

    
    BorderLayoutData nor = new BorderLayoutData(LayoutRegion.NORTH,80);
    nor.setMargins(new Margins(0,5,5,5));
   
    BorderLayoutData cen = new BorderLayoutData(LayoutRegion.CENTER);
    cen.setMargins(new Margins(0,5,0,5));
  
    LayoutContainer cpe = new LayoutContainer();
    cpe.setLayout(new BorderLayout());
    
    ContentPanel cpf = new ContentPanel();
    cpf.setHeaderVisible(false);
     
    store2 = new ListStore<ModelData>();  
    
    grid2 = new EditorGrid<ModelData>(store2, createColumnModel());
    grid2.setBorders(false);  
  ///  grid2.setAutoExpandColumn("artcodigo");  
    grid2.setBorders(true);
    

    store3 = new ListStore<ModelData>();  

    grid3 = new EditorGrid<ModelData>(store3, createColumnModel());  
    grid3.setBorders(false);  
   // grid3.setAutoExpandColumn("artcodigo");  
    grid3.setBorders(true);  
    
        
    new GridDragSource(grid);  
    new GridDragSource(grid2);  
    new GridDragSource(grid3);  
     
    target = new GridDropTarget(grid);
    target.setAllowSelfAsSource(false);  
  
    target2 = new GridDropTarget(grid2);  
    target2.setAllowSelfAsSource(false);

    
    target3 = new GridDropTarget(grid3);  
    target3.setAllowSelfAsSource(false);  
  
    setStyleAttribute("margin", "10px");
    
///para hacer que se seleccione toda la fila	 
    grid2.getSelectionModel().deselectAll();  
    grid3.getSelectionModel().deselectAll();  

    grid2.setSelectionModel(new GridSelectionModel<ModelData>());  
    grid3.setSelectionModel(new GridSelectionModel<ModelData>());  
///    
    cp.add(grid);
    cp.setLayout(new FitLayout());
    
    cpe.add(grid2,nor);
	cpe.add(grid3,cen);
	
	RecetaListForm rlf = new RecetaListForm();
    
	add(rlf,west);
    add(grid,center);
    add(cpe,east);
    setBottomComponent(buttonBar());
  }
   
  public void getStoreGrid2(){
	 ModelData  md2 = (ModelData) store2.getModels().get(0);
	 Receta r = new Receta();
	 r.setArticulopadre_id(Integer.parseInt(md2.get("id").toString()));
	 r.setCantidad(Double.parseDouble(md2.get("stockfisico").toString()));
	
	List<DetalleReceta> listDR = new ArrayList<DetalleReceta>(); 
	DetalleReceta dr;
	for(ModelData md3:store3.getModels()){
	  dr = new DetalleReceta();
	  dr.setArticuloreceta_id(Integer.parseInt(md3.get("id").toString()));
	  dr.setCantidad(Double.parseDouble(md3.get("stockfisico").toString()));
	  listDR.add(dr);
     }	
	Recetas.guardar(r, listDR);
	store2.commitChanges();
	store3.commitChanges();
	
//	Articulos.loaderCombo.load();
//	Recetas.loader.load();
//	btnSave.setEnabled(false);
//	btnNewRecipe.setEnabled(true);
  }
   
   
   public ButtonBar buttonBar(){
	ButtonBar bb = new ButtonBar();
	btnSave = new Button("Guardar");
	btnSave.setSize(100, 30);
	btnSave.setIconStyle("images/icons/save_all.png");
	btnSave.setBorders(false);
	btnSave.addSelectionListener(new SelectionListener<ButtonEvent>(){
	  public void componentSelected(ButtonEvent ce){
        if(store2.getModels().size()>0 && store3.getModels().size()>0)
		 getStoreGrid2();   
   	   store2.removeAll();
	   store3.removeAll();
	   Articulos.loaderCombo.load();
	   Recetas.loader.load();
	   }
	  });
/*	btnNewRecipe = new Button("Nueva Receta");
	btnNewRecipe.setSize(100, 30);
	btnNewRecipe.setIconStyle("images/icons/new.gif");
	btnNewRecipe.setBorders(false);
	btnNewRecipe.addSelectionListener(new SelectionListener<ButtonEvent>(){
	  public void componentSelected(ButtonEvent ce){
		 store2.removeAll();
		 store3.removeAll();
		 btnNewRecipe.setEnabled(false);
		 btnSave.setEnabled(true);
	   }
	  });*/
	HorizontalAlignment align =  HorizontalAlignment.RIGHT;
	bb.add(btnSave);
//	bb.add(btnNewRecipe);
	bb.setAlignment(align);
	bb.setBorders(false);
	return bb;
   }
  
  private ColumnModel createColumnModel() {  
	List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
//id,codarticulo,descripcion,unddescripcion,tartdescripcion,stockfisico
    ColumnConfig col = new ColumnConfig("codarticulo","Codigo Articulo",100);
    conf.add(col);  
    col = new ColumnConfig("descripcion","Articulo",100);
    conf.add(col);  
    col = new ColumnConfig("tartdescripcion","Tipo Articulo",100);
    conf.add(col); 
    col = new ColumnConfig("unddescripcion","Unidad",50);
	conf.add(col);    
    col = new ColumnConfig("stockfisico","Cantidad",100);
    col.setEditor(new CellEditor(new NumberField()));
    conf.add(col);  
    ColumnModel cm = new ColumnModel(conf);   
	  
    return cm;  
  }  
  
}  